home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / str2bmp / str2bmp.cpp < prev    next >
C/C++ Source or Header  |  1993-06-13  |  16KB  |  549 lines

  1. // Code for converting BMPs to strings and strings to Bitmaps.
  2. //     (C) 1993, Balboa Technical Services.
  3. //     
  4. //  
  5. //
  6.  
  7. // Header files.
  8. #include <windows.h>
  9. #include "str2bmp.h"
  10. #pragma hdrstop
  11.  
  12. // DESCRIPTION:
  13. //     DLL Initialization code.
  14. // PARAMETERS:
  15. //     Windows stuff.
  16. // RETURN VALUE:
  17. //     1 if succesfully initialization, else 0.
  18. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  19.  { return 1;
  20.  }
  21.  
  22. // PARAMETERS: Windows stuff.
  23. // RETURN VALUE: 1 if succesfully initialization, else 0.
  24. //int FAR PASCAL WEP (int nParameter)
  25. // { // Dll removal successful.
  26. //   return 1;
  27. // }
  28.  
  29. //    DESCRIPTION:
  30. //        Function for converting VB bitmap to string.
  31. //    PARAMETERS:
  32. //        hdc : Picture.hdc
  33. //           himage : Picture.image
  34. //           string : VB string. Must be large enough to hold data. See
  35. //                         Bmp_GetSize for more info.
  36. //    RETURN VALUE:
  37. //        Number of pixels in the bitmap.
  38. //
  39. DLL_LONG BmpToString( HDC hdc, HBITMAP hImage, STRINGPTR string)
  40.  { // Local variables.
  41.    WORD bw, bh;           // Byte width and byte height.
  42.    HBITMAP hOldBitmap;
  43.  
  44.    // Get information about bitmap size.
  45.    BITMAP bm;
  46.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  47.  
  48.    // Select VB's picture...
  49.    hOldBitmap = (HBITMAP) SelectObject( hdc, hImage);
  50.  
  51.    // Store the width and height in a header at beginning of string
  52.    StringBmp_SetDimensions( string, bm.bmWidth, bm.bmHeight, bm.bmWidthBytes);
  53.  
  54.    // Copy bitmap data into string.
  55.    GetBitmapBits( hImage, bm.bmWidthBytes * bm.bmHeight, string+sizeof( SB));
  56.  
  57.    // DeSelect VB's picture.
  58.    SelectObject( hdc, hOldBitmap);
  59.  
  60.    // Return size in pixels of the bitmap.
  61.    return (bw*bh);
  62.  }
  63.  
  64. //    DESCRIPTION:
  65. //       Function for converting string to a bitmap.
  66. //    PARAMETERS:
  67. //          string : VB string. Must contain valid bitmap info created using
  68. //                        BmpToString
  69. //       hdcSrc : VB Source Picture.hdc
  70. //          hbmpSrc : VB Source Picture.image
  71. //    RETURN VALUE:
  72. //       ON SUCCESS: Number of pixels in the image.
  73. //       ON FAILURE: -1 if string does not contain valid bitmap info.
  74. //
  75. DLL_LONG StringToBmp( STRINGPTR string, HDC hdcSrc, HBITMAP hbmpSrc)
  76.  { // Local variables.
  77.    HBITMAP hOldBitmapNew;
  78.    HBITMAP hOldBitmapSrc;
  79.    HBITMAP hNewBitmap;
  80.    HDC hdcNew;
  81.    WORD bw, bh, bwb;
  82.    int ret;
  83.  
  84.    // Make sure parameter is bitmap string.
  85.    //   Return -1 if string does not contain valid bitmap info.
  86.    if( (ret = StringBmp_ValidateStringBmp( string))<0)
  87.     { return ret;                   
  88.     }
  89.  
  90.    // Get width and height from string header.
  91.    bw = StringBmp_GetWidth(string);
  92.    bh = StringBmp_GetHeight(string);
  93.    bwb = StringBmp_GetWidthBytes(string);
  94.       
  95.    // Create a new bitmap.
  96.    hdcNew = CreateCompatibleDC( hdcSrc);
  97.    hOldBitmapSrc = (HBITMAP) SelectObject( hdcSrc, hbmpSrc);
  98.    hNewBitmap = CreateCompatibleBitmap( hdcSrc, bw, bh);
  99.  
  100.    // Select the new bitmap.
  101.    hOldBitmapNew = (HBITMAP) SelectObject( hdcNew, hNewBitmap);
  102.  
  103.    // Copy bitmap data from string.
  104.    SetBitmapBits( hNewBitmap, bwb*bh, string+sizeof( SB));
  105.  
  106.    // Copy the new bitmap to VB's
  107.    BitBlt( hdcSrc, 0,0,bw,bh, hdcNew, 0,0, SRCCOPY);
  108.  
  109.    // DeSelect bitmaps.
  110.    SelectObject( hdcSrc, hOldBitmapSrc);
  111.    SelectObject( hdcNew, hOldBitmapNew);
  112.  
  113.    // Delete the new bitmap.
  114.    DeleteObject( hNewBitmap);
  115.    DeleteDC( hdcNew);
  116.  
  117.    // Return bitmap size in pixels.
  118.    return (bwb*bh);
  119.  }
  120.  
  121. //    DESCRIPTION:
  122. //        Return required size in bytes for a string to hold a bitmap.
  123. //    PARAMETERS:
  124. //           hImage : VB Picture.image
  125. //    RETURN VALUE:
  126. //        ON SUCCESS: Number of bytes to initialize a string size. Use the
  127. //               following code in VB:
  128. //                 Dim bytes as long, a$
  129. //                 bytes = Bmp_GetSize( Picture1.Image)
  130. //                 if bytes = 0 then stop ' Error! Picture too big.
  131. //                 a$ = spaces( bytes) ' Initialize string size.
  132. //        ON FAILURE: 0 if bitmap has more than 32000 pixels.
  133. //
  134. DLL_LONG Bmp_GetSize( HBITMAP hImage)
  135.  { // Local variables.
  136.    HBITMAP hOldBitmap;
  137.    DWORD bwB, hB;
  138.  
  139.    // Get information about bitmap size.
  140.    BITMAP bm;
  141.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  142.  
  143.    bwB = bm.bmWidthBytes;
  144.    hB = bm.bmHeight;
  145.  
  146.  
  147.    // Check for error condition
  148.    if( (bwB * hB + sizeof( SB) )>32000L)
  149.     { return 0;
  150.     }
  151.  
  152.    // Calculate size of string
  153.    return( sizeof( SB) + hB * bwB);
  154.  
  155.  }
  156.  
  157. //    DESCRIPTION:
  158. //        Returns the height in pixels of a bitmap.
  159. //    PARAMETERS:
  160. //           hImage : VB Picture.image
  161. //    RETURN VALUE:
  162. //           Height in pixels.
  163. //
  164. DLL_LONG Bmp_GetHeight( HBITMAP hImage)
  165.  { // Get information about bitmap size.
  166.    BITMAP bm;
  167.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  168.    return ( bm.bmHeight);
  169.  }
  170.  
  171. //    DESCRIPTION:
  172. //        Returns the width in pixels of a bitmap.
  173. //    PARAMETERS:
  174. //           hImage : VB Picture.image
  175. //    RETURN VALUE:
  176. //           Width in pixels.
  177. //
  178. DLL_LONG Bmp_GetWidth( HBITMAP hImage)
  179.  { // Get information about bitmap size.
  180.    BITMAP bm;
  181.    GetObject( hImage, sizeof( BITMAP), (LPSTR) &bm);
  182.    return( bm.bmWidthBytes);
  183.  }
  184.  
  185.  
  186. //    DESCRIPTION:
  187. //        Store the dimensions of the bitmap in a header at the beginning
  188. //              of the string.
  189. //    PARAMETERS:
  190. //           A : VB string
  191. //        w : width of VB bitmap to be stored in the string.
  192. //        h : height of VB bitmap to be stored in the string.
  193. //        wb : width in bytes (always even) of VB bitmap to be stored.
  194. //    RETURN VALUE:
  195. //        ON SUCCESS: 1
  196. //        ON FAILURE: -1 = Illegal format bitmap string.
  197. DLL_INT StringBmp_SetDimensions( STRINGPTR A, int w, int h, int wb)
  198.  { union StringBmpType temp;
  199.    temp.vbstring = A;
  200.    temp.stringbmp->identifier = StringBmpIdentifier;
  201.    temp.stringbmp->width = w;
  202.    temp.stringbmp->height = h;
  203.    temp.stringbmp->widthbytes = wb;
  204.    return 1;
  205.  }
  206.  
  207.  
  208. //    DESCRIPTION:
  209. //        Examine the header of a string containing bitmap info and
  210. //              determine the bitmap width.
  211. //    PARAMETERS:
  212. //           A : VB string containing bitmap info.
  213. //    RETURN VALUE:
  214. //        width in pixels of the stored bitmap.
  215. DLL_INT StringBmp_GetWidth( STRINGPTR A)
  216.  { union StringBmpType temp;
  217.    temp.vbstring = A;
  218.    return temp.stringbmp->width;
  219.  }
  220.  
  221. //    DESCRIPTION:
  222. //        Examine the header of a string containing bitmap info and
  223. //              determine the bitmap height.
  224. //    PARAMETERS:
  225. //           A : VB string containing bitmap info.
  226. //    RETURN VALUE:
  227. //        Height in pixels of the stored bitmap.
  228. DLL_INT StringBmp_GetHeight( LPSTR A)
  229.  { union StringBmpType temp;
  230.    temp.vbstring = A;
  231.    return temp.stringbmp->height;
  232.  }
  233.  
  234. //    DESCRIPTION:
  235. //        Examine the header of a string containing bitmap info and
  236. //              determine the bitmap width in bytes. (Always even).
  237. //    PARAMETERS:
  238. //           A : VB string containing bitmap info.
  239. //    RETURN VALUE:
  240. //        Width in bytes of the stored bitmap.
  241. DLL_INT StringBmp_GetWidthBytes( LPSTR A)
  242.  { union StringBmpType temp;
  243.    temp.vbstring = A;
  244.    return temp.stringbmp->widthbytes;
  245.   }
  246.  
  247. //    DESCRIPTION:
  248. //        Examine the header of a string containing bitmap info and
  249. //              return the "identifier word". This word is used to check
  250. //              whether the string contains valid bitmap info.
  251. //    PARAMETERS:
  252. //           A : VB string containing bitmap info.
  253. //    RETURN VALUE:
  254. //        Identifier word. Should be 'SB' (0x5342)
  255. DLL_INT StringBmp_GetIdentifier( STRINGPTR A)
  256.  { union StringBmpType temp;
  257.    temp.vbstring = A;
  258.    return temp.stringbmp->identifier;
  259.   }
  260.  
  261. //    DESCRIPTION:
  262. //        Examine the header of a string containing bitmap info and
  263. //              determine whether the bitmap contains valid bitmap info.
  264. //    PARAMETERS:
  265. //           A : VB string containing bitmap info.
  266. //    RETURN VALUE:
  267. //        ON SUCCESS: Return 0.
  268. //        ON FAILURE: -1 = invalid bitmap info.
  269. DLL_LONG StringBmp_Vali